package com.yc.biz.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yc.bean.Types;
import com.yc.biz.TypesBiz;
import com.yc.dao.DBHelper;

public class TypesBizImpl implements TypesBiz {
	
	DBHelper db = new DBHelper();
	
	@Override
	public List<Types> getTypes(int page) {
		String sql = "select * from type limit " + (page-1)*10 + "," + 10;	//从第n条开始,查10条数据
		List<Types> types = db.find(sql, null, Types.class);
		return types;
	}

	@Override
	public int type_modify(int tid, String tname) {
		String sql = "UPDATE type SET tname=? WHERE tid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(tname);
		params.add(tid);
		return db.doUpdate(sql, params);
	}

	@Override
	public int type_del(int tid) {
		int result = -1;
		String sql = "select(1) from good where tid="+tid;
		result = db.findAll(sql, null).size();
		if(result>0){
			sql = "DELETE FROM good WHERE tid="+tid;
			db.doUpdate(sql, null);
		}
		sql = "DELETE FROM type WHERE tid="+tid;
		result = db.doUpdate(sql,null);
		return result;

	}

	@Override
	public int type_add(String tname) {
		String sql = "INSERT INTO TYPE VALUES(0,'"+tname+"')";
		return db.doUpdate(sql, null);
	}

	@Override
	public int getTotalPage() {
		String sql = "SELECT (1) FROM type";
		int total = db.findAll(sql, null).size();
		int totalPage = total/10+1;
		return totalPage;
	}

	@Override
	public List<Types> getAllTypes() {
		String sql = "select * from type";
		List<Types> types = db.find(sql, null, Types.class);
		return types;
	}
}
